package io.renren.modules.doc.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.doc.entity.DocumentEntity;
import io.renren.modules.doc.service.DocumentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 文档管理
 */
@RestController
@RequestMapping("/doc/document")
public class DocumentController {
    @Autowired
    private DocumentService documentService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("doc:document:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = documentService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("doc:document:info")
    public R info(@PathVariable("id") Long id){
        DocumentEntity document = documentService.getDocumentDetail(id);
        return R.ok().put("document", document);
    }

    /**
     * 保存或更新
     */
    @PostMapping("/save")
    @RequiresPermissions("doc:document:save")
    public R save(@RequestBody DocumentEntity document){
        boolean success = documentService.saveOrUpdate(document);
        return success ? R.ok() : R.error("保存失败");
    }

    /**
     * 发布文档
     */
    @PostMapping("/publish/{id}")
    @RequiresPermissions("doc:document:update")
    public R publish(@PathVariable("id") Long id){
        documentService.publishDocument(id);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("doc:document:delete")
    public R delete(@RequestBody Long[] ids){
        documentService.deleteDocument(ids);
        return R.ok();
    }
}
